package user;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import ajaxmethods.SessionMethod;

public class Login extends SessionMethod {

	
	@Override
	public void perform(HttpServletRequest request,HttpServletResponse response)
	{
		super.perform(request, response);
		try {
			StringBuffer sb = loginDlg("");
			CreateAccount createAcct = new CreateAccount();
			
			sb.append(createAcct.createAcctJavascript(""));
			new JSONObject().put("login",sb.toString()).write(response.getWriter());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public StringBuffer loginDlg(String redirect)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<div id=\"loginDlg\">");
		if(redirect.equals("savesearch"))
			sb.append("<div style=\"width:200px;padding-bottom:10px;\">To save this search you must first <a>sign in</a> or <a onClick=\"createAcct();\">create a new account</a></div>");
		else
			sb.append("<div style=\"width:200px;padding-bottom:10px;\"><span style=\"font:12px Arial;\">Don't have an account?</span><a style=\"font:12px Arial;padding-left:4px;\" onClick=\"createAcct();\">sign up</a></div>");

		return sb.append("<input type=\"hidden\" name=\"redirect\" value=\""+redirect+"\"><p class=\"textbox\">"+
			    "<label for=\"username\">Email</label>"+
			    "<input type=\"text\" id=\"username\" class=\"esoinput\" name=\"email\" value=\"\" title=\"username\" tabindex=\"4\">"+
			    "</p><p class=\"textbox\">"+
			    "<label for=\"password\">Password</label>"+
			    "<input type=\"password\" id=\"password\" class=\"esoinput\" name=\"password\" value=\"\" title=\"password\" tabindex=\"5\">"+
			    "</p>"+
			    "<p class=\"remember\">"+
			    "<button type=\"submit\" class=\"esobutton\" onclick=\"doPostWithInputs('user.SubmitLogin','#bubble input')\" style=\"padding:2px 15px;\" id=\"signinbutton\" tabindex=\"7\">Sign In</button>"+
			    "<input type=\"checkbox\" id=\"remember\" name=\"remember_me\" value=\"1\" tabindex=\"6\">"+
			    "<label for=\"remember\">Remember me</label>"+
				"</p></div>" +
				"<script>" +
				"$(document).ready(function(){$('input[name=\"password\"]').keypress(function(e){var code = (e.keyCode? e.keyCode : e.which); if(code==13) doPostWithInputs('user.SubmitLogin','#bubble input'); }); });</script>");
	}
}
